package com.xiangge.number;

import java.math.BigDecimal;

/**
 * @author xiangge
 * @version 1.0
 * @date 2025/4/14 14:00
 * @description BigDecimal类
 *       描述： BigDecimal类，专门用来处理浮点数精度问题的。是Number 抽象类的子类。
 *       创建对象： public BigDecimal(String val)
 *       成员方法：
 *          public BigDecimal abs():
 *          public BigDecimal add(BigDecimal augend):
 *          public BigDecimal subtract(BigDecimal subtrahend):
 *          public BigDecimal multiply(BigDecimal multiplicand):
 *          public BigDecimal divide(BigDecimal divisor):
 *          public BigDecimal remainder(BigDecimal divisor):
 *          public BigDecimal divideAndRemainer(BigDecimal divisor):
 *
 *           public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode):
 *                 BigDecimal divisor : 被除数
 *                 int scale ： 表示保留几位小数
 *                 int roundingMode : 表示精度的舍入模式   魔法值
 *                     public final static int ROUND_UP =            0;    向上取整
 *                     public final static int ROUND_DOWN =          1;    向下取整
 *                     public final static int ROUND_HALF_UP =       4;    四舍五入
 *                     public final static int ROUND_HALF_DOWN =     5;    五舍六入
 */
public class NumberDemo6 {
    public static void main(String[] args) {
        BigDecimal bigDecimal1 = new BigDecimal("0.123456789");

        BigDecimal divide = bigDecimal1.divide(new BigDecimal("1"), 4, BigDecimal.ROUND_HALF_UP);
        System.out.println("divide = " + divide);// divide = 0.1235
    }
    public static void main2(String[] args) {
        BigDecimal num1 = new BigDecimal("0.3");
        BigDecimal num2 = new BigDecimal("0.4");

        // 绝对值
        BigDecimal num3 = num1.abs();
        System.out.println("num3 = " + num3);

        // 加法
        BigDecimal num4 = num1.add(num2);
        System.out.println("num4 = " + num4);
        System.out.println("num4 = " + num4.toString());

        // 减法
        BigDecimal num5 = num1.subtract(num2);
        System.out.println("num5 = " + num5);

        // 乘法
        BigDecimal num6 = num1.multiply(num2);
        System.out.println("num6 = " + num6);

        // 除法
        BigDecimal num7 = num1.divide(num2);
        System.out.println("num7 = " + num7);

        // 取余
        BigDecimal num8 = num1.remainder(num2);
        System.out.println("num8 = " + num8);

        // 除法和取余
        BigDecimal[] num9 = num1.divideAndRemainder(num2);
        System.out.println(num9.length);
        System.out.println("num9[0] = " + num9[0]);
        System.out.println("num9[1] = " + num9[1]);
    }
    public static void main1(String[] args) {
        float num1 = 0.3f;
        float num2 = 0.4F;
        float num3 = num1 + num2;
        System.out.println(num3); // 0.70000005
        BigDecimal num4 = new BigDecimal(num1);
    }
}
